package cn.innosmart.aq.customize;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.innosmart.aq.R;

/**
 * Created by sabonis on 6/25/15.
 * With Clean Code in mind
 */
public class HorizonSelector extends FrameLayout {

    private List<String> items = new ArrayList<>();
    private int index;
    private TextView mItemTextView;
    private AdapterView.OnItemSelectedListener mOnItemSelectedListener;

    public HorizonSelector(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.widget_horizon_selector, this, true);
    }

    public List<String> getItems() {
        return items;
    }

    public void setItems(int res) {
        setItems(Arrays.asList(getContext().getResources().getStringArray(res))
        );
    }

    public void setItems(List<String> items) {
        this.items = items;
        init();
    }

    public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener onItemSelectedListener) {
        mOnItemSelectedListener = onItemSelectedListener;
    }

    private void init() {
//        mItemTextView = (TextView) findViewById(R.id.item);
        mItemTextView.setText(items.get(index));

        findViewById(R.id.btn_left).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (items.size() == 1) {
                    if (mOnItemSelectedListener != null) {
                        mOnItemSelectedListener.onItemSelected(null, getRootView(), -1, 0);
                    }
                } else {
                    select(--index);
                }
            }
        });

        findViewById(R.id.btn_right).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (items.size() == 1) {
                    if (mOnItemSelectedListener != null) {
                        mOnItemSelectedListener.onItemSelected(null, getRootView(), 1, 0);
                    }
                } else {
                    select(++index);
                }

            }
        });
    }

    private void select(int position) {
        if (position < 0) {
            index = items.size() - 1;
//            return;
        }
        if (position >= items.size()) {
            index = 0;
//            return;
        }
        System.out.println("items.get="+items.get(index));
        mItemTextView.setText(items.get(index));
        if (mOnItemSelectedListener != null) {
            mOnItemSelectedListener.onItemSelected(null, this, index, 0);
        }
    }


    public int getSelected() {
        return index;
    }

    public void setSelected(int position) {
        index=position;
        select(position);
    }

}
